home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Generic Lib / Demo Generic Lib.rsrc / MODL_8227_Holding Tank < prev    next >
Encoding:
Text File  |  1994-06-22  |  8.8 KB  |  351 lines

  1. integer    initFlag, discrete, resetCon;
  2. integer    newInput, newWant;
  3. real    oldTime, maxVal, lastReset;
  4. real    maxContents, maxTime;
  5. real    timeArray[];
  6. real    delayedInput;        // from prev step for backward integration
  7. real    delayedwantIn;        // from prev step for backward integration
  8.  
  9. //    This block accumulates values and allows a specified
  10. //        amount to be withdrawn
  11. //    Copyright © 1989-1994 by Imagine That, Inc.
  12. //    All Rights Reserved.
  13. //    Extend Generic Library, Holding Tank block; Alfy Riddle
  14. //        modified     3/1/92  JSL modified extensively for V2.0;
  15. //                    4/1/92 BD for backward or forward euler
  16. //                    12/8/92 JSL added currentStep == 0 to for reset
  17. //                    1/14/92 JSL getOut Message handler doesn't recalc
  18. //                    1/20/93 JSL changed inputCon to input val in calc
  19. //                    1/20/93 JSL set contentsOut to not recalc from plotter
  20. //                    1/20/93 JSL changed connector names
  21. //                    2/26/93 JSL added sendMsgToOutputs(ConIn) to on conOut
  22. //                    2/26/93 JSL added lastReset
  23. //                    3/15/93 JSL replaced inputCon with inputVal
  24. //                     10/15/93 BD changed integration to Euler standard
  25. //                     10/26/93 BD added Euler forward for science
  26. //                     10/27/93 BD fixed animation level
  27. //                    2/14/94  DJK modified trace and report
  28. //                    3/10/94  DJK added block label to report & trace
  29. //                    5/18/94  BD changed dialog, help and fixed bug in forward
  30.  
  31.  
  32. procedure calc()
  33. {
  34.     real    numRemoved;            // number requested to remove
  35.     real    timeStep;            // for integration
  36.     real    inputVal, wantVal;
  37.     
  38.     if (OpIntegrate || OpIntegrateSci)    // for integration
  39.         {
  40.         if (OpIntegrateSci)                // for science
  41.             {
  42.             delayedInput = ConIn;        // use forward Euler
  43.             delayedwantIn = wantIn;
  44.             }
  45.  
  46.         if (noValue(delayedInput))
  47.             delayedInput = 0.0;
  48.         
  49.         if (noValue(delayedwantIn))
  50.             delayedwantIn = 0.0;
  51.  
  52.         timeStep = CurrentTime-oldTime;    // allows use in discrete evt
  53.         
  54.         if( ((currentStep == 0 && !resetCon) || resetIn > 0.5)  && lastReset != currentTime)
  55.             {
  56.             if( initFlag )
  57.                 {
  58.                 if( noValue(initIn) )
  59.                     {
  60.                     userError("BLANK value at s input in Holding Tank block number "+(MyBlockNumber()));
  61.                     abort;
  62.                     }
  63.  
  64.                 contents = initIn; // use connector for starting value if connected
  65.                 levelInit = contents;
  66.                 }
  67.             else
  68.                 contents = levelInit;    // use dialog box value
  69.  
  70.             lastReset    = currentTime;
  71.             
  72.             if (OpIntegrateSci)        // 5/18/94 BD if forward integration
  73.                 {
  74.                 // integrate input, numRemoved
  75.                 inputVal = delayedInput*timeStep;
  76.                 numRemoved = delayedwantIn*timeStep;
  77.                 }
  78.             else                    // needs inputs zeroed
  79.                 {
  80.                 inputVal    = 0.0;
  81.                 numRemoved    = 0.0;
  82.                 }
  83.             }
  84.         else
  85.             {
  86.             // integrate input, numRemoved
  87.             inputVal = delayedInput*timeStep;
  88.             numRemoved = delayedwantIn*timeStep;
  89.             }
  90.  
  91.         oldTime = CurrentTime;
  92.         }
  93.     else
  94.         {
  95.         if ( ((currentStep == 0 && !resetCon) || resetIn > 0.5)  && lastReset != currentTime)    // 12/8/92 Added currentStep
  96.             {
  97.             if( initFlag )
  98.                 {
  99.                 if( noValue(initIn) )
  100.                     {
  101.                     userError("BLANK value at s input in Holding Tank block number "+(MyBlockNumber()));
  102.                     abort;
  103.                     }
  104.                 contents = initIn; // use connector for starting value if connected
  105.                 levelInit = contents;
  106.                 }
  107.             else
  108.                 contents = levelInit;    // use dialog box value
  109.             lastReset = currentTime;
  110.             }
  111.  
  112.         //    check input connectors for NoValues
  113.         if(noValue(wantIn))    // NANs do not change contents
  114.             numRemoved = 0.0;
  115.         else
  116.             numRemoved = wantIn;    // if sum, timestep is 1.0
  117.     
  118.         if(noValue(ConIn))    // NANs do not change contents
  119.             inputVal = 0.0;
  120.         else
  121.             inputVal = conIn;    // if sum, timestep is 1.0
  122.         }
  123.  
  124.     if (discrete && !newInput)
  125.         inputVal = 0.0;
  126.     newInput = FALSE;
  127.  
  128.     if (discrete && !newWant)
  129.         numRemoved = 0.0;
  130.     newWant = FALSE;
  131.  
  132.     if((contents + inputVal > numRemoved) OR canGoNeg)    // lower limit at zero?
  133.         contents += (inputVal - numRemoved);            // ok for integration
  134.     else
  135.         {
  136.         numRemoved = contents+inputVal;
  137.         if (numRemoved < 0)
  138.             numRemoved = 0;
  139.         contents = 0;
  140.         }
  141.     
  142.     if (contents > maxVal)        // increase maximum if necessary
  143.         maxVal = contents;
  144.     if (contents > maxContents)    // this will remember a smaller max level
  145.         maxContents = contents;    // remember max level from this run
  146.         
  147.     animationShow(1);
  148.     if (maxVal <= 0.0)
  149.         animationLevel(1, 0);
  150.     else
  151.         animationLevel(1, contents/ maxVal);
  152.  
  153.     getOut = numRemoved;
  154.     contentsOut = contents;
  155.  
  156.     // sysGlobal2 is the file reference number for the DEBUG TRACE
  157.     if( sysGlobal2 != 0.0 ) // 0 is error, check for open file for TRACE
  158.         {
  159. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  160.         fileWrite(sysGlobal2,"Holding Tank                 block number "+(MyBlockNumber())+".  Current Time:"+currentTime+".","",True);
  161.         if(getBlockLabel(myBlockNumber()) != "")
  162.             fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  163.         fileWrite(sysGlobal2,"     Input = "+conIn,"",True);
  164.         fileWrite(sysGlobal2,"     Contents = "+contentsOut,"",True);
  165.         fileWrite(sysGlobal2,"     Output Wanted = "+wantIn,"",True);
  166.         fileWrite(sysGlobal2,"     Output = "+getOut,"",True);
  167.         fileWrite(sysGlobal2,"     Start Input = "+initIn,"",True);
  168.         fileWrite(sysGlobal2,"     Reset Input = "+resetIn,"",True);
  169.         fileWrite(sysGlobal2," ","",True);
  170.         }
  171.         
  172.     if (OpIntegrate)
  173.         {
  174.         delayedInput = conIn;        // 4/1/92 BD backward
  175.         delayedwantIn = wantIn;        // 4/1/92 BD backward
  176.         }
  177. }
  178.  
  179. // These connector messages are only called when this block is used in a 
  180. // Discrete Event Model.
  181.  
  182. on ConIn
  183. {
  184.     sendMsgToOutputs(resetIn);
  185.     sendMsgToOutputs(initIn);
  186.     sendMsgToOutputs(wantIn);
  187.     newInput = TRUE;
  188.     calc();
  189.     sendMsgToInputs(contentsOut);
  190.     sendMsgToInputs(getOut);
  191. }
  192.  
  193. // These connector messages are only called when this block is used in a 
  194. // Discrete Event Model.
  195.  
  196. on ResetIn
  197. {
  198.     sendMsgToOutputs(ConIn);
  199.     sendMsgToOutputs(initIn);
  200.     sendMsgToOutputs(wantIn);
  201.     calc();
  202.     sendMsgToInputs(contentsOut);
  203.     sendMsgToInputs(getOut);
  204. }
  205.  
  206. // These connector messages are only called when this block is used in a 
  207. // Discrete Event Model.
  208.  
  209. on wantIn
  210. {
  211.     sendMsgToOutputs(resetIn);
  212.     sendMsgToOutputs(initIn);
  213.     sendMsgToOutputs(ConIn);
  214.     newWant = TRUE;
  215.     calc();
  216.     sendMsgToInputs(contentsOut);
  217.     sendMsgToInputs(getOut);
  218. }
  219.  
  220. // These connector messages are only called when this block is used in a 
  221. // Discrete Event Model.
  222.  
  223. on initIn
  224. {
  225.     sendMsgToOutputs(resetIn);
  226.     sendMsgToOutputs(ConIn);
  227.     sendMsgToOutputs(wantIn);
  228.     calc();
  229.     sendMsgToInputs(contentsOut);
  230.     sendMsgToInputs(getOut);
  231. }
  232.  
  233. // These connector messages are only called when this block is used in a 
  234. // Discrete Event Model.
  235.  
  236. on contentsOut
  237. {
  238.     //  if the message is not coming from a plotter
  239.     if (sysGlobalint9 == 0)
  240.         {
  241.         sendMsgToOutputs(conIn);
  242.         sendMsgToOutputs(resetIn);
  243.         sendMsgToOutputs(initIn);
  244.         sendMsgToOutputs(wantIn);
  245.         calc();
  246.         sendMsgToInputs(getOut);
  247.         }
  248. }
  249.  
  250. // These connector messages are only called when this block is used in a 
  251. // Discrete Event Model.
  252.  
  253. on getOut
  254. {
  255. // this message handler doesn't recalc anymore.
  256. }
  257.  
  258.  
  259. // This message occurs for each step in the simulation.
  260. on simulate
  261. {
  262.     calc();
  263. }
  264.  
  265.  
  266. // If the dialog data is inconsistent for simulation, abort.
  267. on checkdata
  268. {
  269.     sysGlobal1 = 0.0;    // prevent false reports
  270.     sysGlobal2 = 0.0;    // prevent false debugs
  271.     
  272.     initFlag    = initIn;
  273.     resetCon    = resetIn;
  274.     
  275.     if( noValue(levelInit) AND !initFlag  )
  276.         {
  277.         userError("Put a value in the Starting Contents in Holding Tank block number "+(MyBlockNumber()));
  278.         abort;
  279.         }
  280. }
  281.  
  282.  
  283. on createBlock
  284. {
  285.     levelInit = 0;
  286.     canGoNeg = 0;
  287.     
  288.     OpSum = TRUE;    // add integration
  289.     maxVal      = 0.0;
  290.     maxContents = 0.0;
  291. }
  292.  
  293.  
  294. // Initialize any simulation variables.
  295. on initsim
  296. {
  297.     contents = levelInit;
  298.  
  299.     maxVal = maxContents;    // start from max remembered from last run
  300.         
  301.     if (maxVal == 0.0)
  302.         maxVal = levelInit;
  303.  
  304.     animationLevel(1, 0);
  305.     animationColor(1, 30000, 30000, 30000, 1);
  306.     if (animationOn)
  307.         animationShow(1);
  308.     else
  309.         animationHide(1, FALSE);
  310.     maxTime            = endTime - startTime;
  311.     maxContents        = 0.0;            // reset it to remember max level
  312.     delayedInput    = 0.0;
  313.     delayedwantIn    = 0.0;
  314.     lastReset         = BLANK;
  315.     
  316.     discrete = FALSE;
  317.     if( getPassedArray(sysGlobal0, timeArray) )
  318.         {
  319.         getSimulateMsgs(FALSE);
  320.         discrete = TRUE;
  321.         }
  322.     newInput = FALSE;
  323.     newWant  = FALSE;
  324.     
  325.     if (discrete)
  326.         oldTime = startTime;            // discrete
  327.     else
  328.         oldTime = startTime-deltaTime;    // continuous
  329. }
  330.  
  331.  
  332. on endSim
  333. {
  334.     // sysGlobal1 is the file reference number for the TEXT REPORT
  335.     if( sysGlobal1 != 0.0 ) // 0 is error, check for open file for REPORT
  336.         {
  337. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  338.         fileWrite(sysGlobal1,"Holding Tank                 block number "+MyBlockNumber(),"",True);
  339.         if(getBlockLabel(myBlockNumber()) != "")
  340.             fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  341.         fileWrite(sysGlobal1,"     Starting Contents = "+levelInit,"",True);
  342.         fileWrite(sysGlobal1,"     Final Contents = "+contents,"",True);
  343.         if( canGoNeg )
  344.             fileWrite(sysGlobal1,"     Output wanted can take tank negative","",True);
  345.  
  346.         if( comments != "" )
  347.             fileWrite(sysGlobal1,"     Comments = "+comments,"",True);        
  348.  
  349.         fileWrite(sysGlobal1," ","",True);
  350.         }
  351. }